from PyQt5.QtGui import QFont, QIcon, QRegExpValidator
from PyQt5.QtCore import Qt, QRegExp
from PyQt5.QtWidgets import QDialog, QApplication, QLabel, QLineEdit, QPushButton, QMessageBox, QFormLayout
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

from qdarkstyle import load_stylesheet_pyqt5

class XiuGai_MiMa_Dialog(QDialog):
    def __init__(self, parent = None):
        super(XiuGai_MiMa_Dialog, self).__init__(parent)
        self.setWindowTitle('天空偏振光分布模式仿真与探测系统')
        self.setWindowModality(Qt.WindowModal)
        self.setup_ui()

    def setup_ui(self):
        self.resize(255, 311)

        self.BiaoTi_Label = QLabel('修改密码')
        font = QFont()
        font.setPixelSize(20)
        self.BiaoTi_Label.setFont(font)
        self.ZhangHao_Label = QLabel('帐    号：')
        self.JiuMiMa_Label = QLabel('旧 密 码：')
        self.XinMiMa_Label = QLabel('新 密 码：')
        self.QueRenMiMa_Label = QLabel('确认密码：')

        font.setPixelSize(16)
        self.ZhangHao_Label.setFont(font)
        self.JiuMiMa_Label.setFont(font)
        self.XinMiMa_Label.setFont(font)
        self.QueRenMiMa_Label.setFont(font)

        self.ZhangHao_LineEdit = QLineEdit()
        font.setPixelSize(16)
        self.ZhangHao_LineEdit.setFont(font)
        self.JiuMiMa_LineEdit = QLineEdit()
        self.XinMiMa_LineEdit = QLineEdit()
        self.QueRenMiMa_LineEdit = QLineEdit()
        font.setPixelSize(16)
        self.JiuMiMa_LineEdit.setFont(font)
        self.XinMiMa_LineEdit.setFont(font)
        self.QueRenMiMa_LineEdit.setFont(font)

        self.ZhangHao_LineEdit.setMaxLength(10)
        self.JiuMiMa_LineEdit.setMaxLength(16)
        self.XinMiMa_LineEdit.setMaxLength(16)
        self.QueRenMiMa_LineEdit.setMaxLength(16)

        self.JiuMiMa_LineEdit.setEchoMode(QLineEdit.Password)
        self.XinMiMa_LineEdit.setEchoMode(QLineEdit.Password)
        self.QueRenMiMa_LineEdit.setEchoMode(QLineEdit.Password)

        self.ZhangHao_LineEdit.setPlaceholderText("数字")
        self.JiuMiMa_LineEdit.setPlaceholderText("大/小英文+数字")
        self.XinMiMa_LineEdit.setPlaceholderText("大/小英文+数字")
        self.QueRenMiMa_LineEdit.setPlaceholderText("与新密码一致")

        self.QueRen_XiuGai_MiMa_Button = QPushButton('确认修改')
        font.setPixelSize(16)
        self.QueRen_XiuGai_MiMa_Button.setFont(font)
        self.QueRen_XiuGai_MiMa_Button.setFixedHeight(32)
        self.QueRen_XiuGai_MiMa_Button.setFixedWidth(100)
        self.QueRen_XiuGai_MiMa_Button.setIcon(QIcon("./images/修改 密码.png"))

        self.FormLayout = QFormLayout()
        self.FormLayout.addRow('', self.BiaoTi_Label)
        self.FormLayout.addRow(self.ZhangHao_Label, self.ZhangHao_LineEdit)
        self.FormLayout.addRow(self.JiuMiMa_Label, self.JiuMiMa_LineEdit)
        self.FormLayout.addRow(self.XinMiMa_Label, self.XinMiMa_LineEdit)
        self.FormLayout.addRow(self.QueRenMiMa_Label, self.QueRenMiMa_LineEdit)
        self.FormLayout.addRow('', self.QueRen_XiuGai_MiMa_Button)

        self.setLayout(self.FormLayout)

        self.BiaoTi_Label.setMargin(8)
        self.FormLayout.setVerticalSpacing(10)

        reg1 = QRegExp("[1-9][0-9]+$")
        YanZhengQi = QRegExpValidator(self)
        YanZhengQi.setRegExp(reg1)
        self.ZhangHao_LineEdit.setValidator(YanZhengQi)

        reg2 = QRegExp("[a-zA-Z0-9]+$")
        YanZhengQi = QRegExpValidator(self)
        YanZhengQi.setRegExp(reg2)
        self.XinMiMa_LineEdit.setValidator(YanZhengQi)
        self.JiuMiMa_LineEdit.setValidator(YanZhengQi)
        self.QueRenMiMa_LineEdit.setValidator(YanZhengQi)

        self.QueRen_XiuGai_MiMa_Button.clicked.connect(self.QueRen_XiuGai_MiMa_Button_clicked)

    def QueRen_XiuGai_MiMa_Button_clicked(self):

        ZhangHao = self.ZhangHao_LineEdit.text()
        JiuMiMa  = self.JiuMiMa_LineEdit.text()
        XinMiMa = self.XinMiMa_LineEdit.text()
        QueRenMiMa = self.QueRenMiMa_LineEdit.text()

        if(ZhangHao=='' or JiuMiMa=='' or XinMiMa=='' or QueRenMiMa==''):
            print(QMessageBox.warning(self, "警告", "请输入完整信息！", QMessageBox.Yes, QMessageBox.Yes))
            return

        ShuJuKu = QSqlDatabase.addDatabase("QSQLITE")
        ShuJuKu.setDatabaseName('./db/Image_database.db')
        ShuJuKu.open()
        query = QSqlQuery()
        Range_ZhangHao = "select * from user where zhanghao = '%s'" % ZhangHao
        query.exec_(Range_ZhangHao)

        if(not query.next()):
            print(QMessageBox.warning(self, '警告：', '查询不到用户，请输入帐号！', QMessageBox.Yes, QMessageBox.Yes))
            self.ZhangHao_LineEdit.clear()
            return

        sql = "SELECT * FROM User WHERE Password='%s' AND ZhangHao='%s'" %(JiuMiMa, ZhangHao)
        query.exec_(sql)
        if(not query.next()):
            print(QMessageBox.warning(self, '提示', '密码错误，请重新输入！', QMessageBox.Yes, QMessageBox.Yes))
            self.JiuMiMa_LineEdit.clear()
            return
        if(XinMiMa!=QueRenMiMa):
            print(QMessageBox.warning(self, '警告', '两次输入密码不同，请重新输入。', QMessageBox.Yes, QMessageBox.Yes))
            self.XinMiMa_LineEdit.clear()
            self.QueRenMiMa_LineEdit.clear()
            return

        GengXinMiMa = "UPDATE User SET Password='%s' WHERE ZhangHao='%s'" % (XinMiMa, ZhangHao)
        query.exec_(GengXinMiMa)
        ShuJuKu.commit()
        QMessageBox.information(self, '提醒', '修改密码成功，请登录系统', QMessageBox.Yes, QMessageBox.Yes)
        self.close()
        return

if __name__ == '__main__':
    from sys import argv, exit
    app = QApplication(argv)
    app.setStyleSheet(load_stylesheet_pyqt5())
    window = XiuGai_MiMa_Dialog()
    window.show()
    exit(app.exec_())